home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
edit
/
me_cd25.zip
/
DOC.ZIP
/
MM2.DOC
< prev
next >
Wrap
Text File
|
1992-11-09
|
30KB
|
961 lines
========================================================================
== The Mutt2 Machine Craig Durland 88, 10/91 ==
========================================================================
Here is info on embedding the Mutt2 machine in a application and the
Mutt2 machine itself.
The Mutt2 Machine is a virtual machine ment to be an embedded language.
It is written in C for ease of porting.
Copyright 1991 Craig Durland
Distributed under the terms of the GNU General Public License.
Distributed "as is", without warranties of any kind, but comments,
suggestions and bug reports are welcome.
========================================================================
== Embedding the Mutt2 Machine ==
========================================================================
Data Types:
maddr
uint8
Example data structures:
block :
typedef struct
{
char *name; /* Name of the block (munged file name) */
uint8 *global_vars; /* Where the global vars are in this block */
maddr code; /* The start of the code in this block */
void *global_object_table;
int num_global_objects;
} CodeBlock;
Programs:
typedef struct /* program address table */
{
maddr addr; /* address of routine */
short int block; /* link to programs code block. 0 => dead */
} Pgm;
To embed the Mutt2 Machine in your C program, write the following
routines:
Function: FILE *MMopen_code_file(char *file_name)
Description:
Open a Mutt code (.mco) file so that it can be loaded by the Mutt
Machine.
Input:
name: Name of code file to open. The extension is ".mco". May have
a leading path.
Returns:
FILE : Pointer to the opened code file.
NULL : Couldn't find the file.
Notes:
You might want to parse a PATH environment variable.
Function: maddr MMpgm_addr(int pgm_id)
Description:
Return the address of the Mutt routine that has pgm_id. Also update
MMglobal_vars, and MMglobal_object_table to point to the proper
areas.
Input:
pgm_id : The id of a Mutt program.
Returns:
Address of the start of the Mutt program code.
Munges:
MMglobal_vars
MMglobal_object_table
Notes:
You only need to update the globals if they will change.
Function: int MMpgm_lookup(char *pgm_name)
Description:
Return the program id of the program that is named pgm_name.
Input:
pgm_name:
Returns:
Program id.
Notes:
Function: void MMblock_name(char *buf, *file_name)
Description:
Create a block name (using file_name if you want) and put it into buf.
Input:
buf:
file_name:
Munges:
buf
Notes:
Function: int MMadd_block(
char *block_name;
maddr code;
uint8 *global_vars;
void *global_object_table;
int num_global_objects)
Description:
Save some information that the Mutt Machine might need to use later.
Input:
block_name: A name returned by MMblock_name. Useful to check to see
if a block is being reloaded.
code: Pointer to the Mutt code. MMpgm_addr() wants this.
global_vars: Pointer to the block-global non-object variables. Also
used by MMpgm_addr().
global_object_table: Pointer to the block-global objects. Used by
MMpgm_addr().
num_global_objects: Number of block-global objects. Used when
calling MMfree_block().
Returns:
A block id that be used in MMadd_pgm(). You need block ids so you can
cross index the program table and the block table. You save to save
memory by keeping a (program-name, block-id) tuple and using the
block table in MMpgm_addr().
Notes:
Function: int MMadd_pgm(char *pgm_name; int block_id; maddr code_addr)
Description:
Add a program to your program table. This so MMpgm_addr() can get the
info.
Input:
pgm_name: Pointer the name of the program. You only need to save a
copy of the pointer, the name itself is in the code block name table.
block_id: Returned by MMadd_block.
code_addr: Start address of the code in the program. Used by
MMpgm_addr().
Returns:
TRUE: Everything went as expected.
FALSE: Ran out of memory or some other real bad thing.
Notes:
Function: void MMset_hooks()
Description:
Hooks for Mutt programs. When certain events occur in the
application, you might want to call a hook so your application can
be extended in useful ways.
Notes:
Called from MM after every Mutt code block is loaded.
Call this at start up to make sure all hooks are
properly initialized to no hook.
Function: void MMbitch(char *message)
Description:
A fatal error has occurred when running a Mutt program. Inform the
user and bail out of the Mutt Machine (usually by calling
MMabort_pgm()).
Input:
message: Text that has some information about what happened. You
might want to add some verbiage saying that the program is aborting.
Notes:
Only the Mutt Machine aborts. The application should be OK (it is up
to the application to decide).
Function: void MMmoan(char *message)
Description:
Inform the user that the Mutt program has errored.
Input:
message: Text that has some information about what happened.
Notes:
This is used for those times when MMabort_pgm() can't be called.
Function: void MMmsg(char *message)
Description:
Give the user a message. Used by MSG: (msg "hoho") will result in
MMmsg("hoho");
Input:
message: Text for the user.
Notes:
Function: void MMask(char *prompt, *buf)
Description:
Ask the user for a response. Used by ASK: (ask "Press a key ") will
result in MMask("Press a key ", &buf);
Input:
prompt:
buf: where to put the reply.
Munges:
buf
Notes:
Function: int MMaux_fcn(char *aux_fcn_name)
Description:
Run a aux function by name. Aux functions are the functions that the
application defines and MM knows nothing about. For example, say
the application is a spread sheet, you have C code that sums a
column and you want the Mutt name for this to be "sum-column". Now,
when a Mutt program does a (sum-column n), MM looks around, can't
find a program named "sum-column", thinks it might be part of the
application and calls MMaux_fcn("sum-column"). Your C code can then
access the Mutt stack (see MMpull_nth_arg()) and sum that column.
Input:
Returns:
TRUE: aux_fcn_name is real and I did it.
FALSE: aux_fcn_name is not something I know about.
Notes:
Function: void MMxtoken(int token)
Description:
Execute a token defined by the application. If you compile a Mutt
program with a token file ("mc2 -t<token-file>"), MM will call
MMxtoken(token). Using the MMaux_fcn() example, if you created a
token file with the entry "123 sum-column" in it, used that file to
compile a Mutt program with (sum-column 3) in it, then, when MM
executes that line, MMxtoken(123) will be called. You then take 123
and figure out you need to call sum_column(3).
Input:
token:
Notes:
MMxtoken() is short hand for MMaux_fcn().
Its assumed that token file is correct - you might want to do some
token checking if this is a bad assumption (it usually is).
Token files can really reduce the (static) space used by a Mutt
program and speed things up (because it gets rid of string lookups).
Token is usually an index into a array of function pointers or a case
in a switch statement.
Function: void MMgc_external_objects()
Description:
Garbage collect all mortal (temporary) objects that may have been
created by Mutt programs and not freed by them.
This is typically called (by the Mutt Machine) after a pgm has been
run or aborted to clean up stuff the programmer forgot to or was
unable to (as in the case where the pgm was aborted).
See also: Section on garbage collection.
Internal Routines of External Interest
-------- -------- -- -------- --------
Routines Mutt extension writers are interested in:
Function: int MMpull_nth_arg(vsitem *val; int n)
Description